package com.xk.springboot.controller;

import com.xk.springboot.dto.ResponseData;
import com.xk.springboot.dto.ResultData;
import com.xk.springboot.pojo.User;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/error")
public class ErrorController {

    @GetMapping("test")
    public ResultData springError(@Validated @RequestBody User user, BindingResult result){
        if (result.hasErrors()) {
            String errors = "";
            List<ObjectError> errors1 = result.getAllErrors();
            for (ObjectError e:errors1
                 ) {
                errors += e.getDefaultMessage()+";";
            }
            return ResponseData.error(1001,errors);
        }
      return ResponseData.success(user);
    }
}
